using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SalesAndStoreApp.DAL.DAO;

namespace SalesAndStoreApp.DAL.Gateway
{
    public class VendorGateway:DbGateway
    {
        public List<Vendor> GetAll()
        {
            try
            {
                var vendors = new List<Vendor>();
                string query = "SELECT * FROM Vendor";
                Command.CommandText = query;
                Connection.Open();
                using (SqlDataReader dataReader = Command.ExecuteReader())
                {
                    while (dataReader.Read())
                    {
                        Vendor vendor = new Vendor();
                        vendor.Id = int.Parse(dataReader["Id"].ToString());
                        vendor.Name = dataReader["Name"].ToString();
                        vendors.Add(vendor);
                    }
                }
                Connection.Close();
                return vendors;
        
            }
            catch (Exception exception)
            {
                
                throw new Exception("Error occurred while trying to retrieve all vendor list",exception);
            }
        }


        public Vendor GetById(int id)
        {
            try
            {
                Connection.Open();
                string query = "SELECT * FROM Vendor WHERE Id =@id";
                Command.CommandText = query;
                Command.Parameters.Clear();
                Command.Parameters.Add("@id", SqlDbType.Int);
                Command.Parameters["@id"].Value = id;
                Vendor vendor = new Vendor();
                using (SqlDataReader dataReader = Command.ExecuteReader())
                {
                    while (dataReader.Read())
                    {
                        vendor.Id = int.Parse(dataReader["Id"].ToString());
                        vendor.Name = dataReader["Name"].ToString();
                    }

                }
                Connection.Close();
                return vendor;
            }
            catch (Exception exception)
            {
                
                throw new Exception("Error occurred while trying to retrive a Vendors information",exception);
            }
         }
    }
}